<template>
	<div class="areaCode_com">
		<van-action-sheet v-model="hasShow" :title="$t('home.selectNetwork')">
			<div class="content">
				<van-loading style="text-align: center; padding: 20px;" v-if="loading" />
				<div class="list_box">
					<div class="item_box" :class="{active: coinId == item.network_id}" v-for="(item,index) in network && network.length > 0 ? network : list"
						:key="index" @click="handleItem(item)">
						{{item.name}}
					</div>
					<div class="no_data" v-if="(network && network.length <= 0 && list && list.length <= 0) && !loading">
						<img src="@/assets/images/home/noData.png" class="img" />
						<p>{{$t('home.noData')}}</p>
					</div>
				</div>
			</div>
		</van-action-sheet>
	</div>
</template>

<script>
	import {
		indexNetwork
	} from '@/api/my'
	export default {
		props: {
			value: {
				type: Boolean,
				default: false,
			},
			coinId: {
				type: String | Number,
				default: '',
			},
			network: {
				type: Array,
				default: function() {
					return []
				},
			}
		},
		components: {},
		data() {
			return {
				loading: false,
				active: {},
				list: [],
			}
		},
		computed: {
			hasShow: {
				get() {
					if (this.network && this.network.length <= 0) {
						this.getList()
					}
					return this.value
				},
				set(val) {
					this.$emit('input', val)
				}
			},
		},
		watch: {},
		mounted() {},
		methods: {
			async getList() {
				this.loading = true;
				const res = await indexNetwork({
					currency_id: this.coinId
				}).catch(() => this.loading = false)
				if (res && res.code == 1) {
					this.list = res.data
				} else {
					this.$toast.fail(res.msg)
				}
				this.loading = false;
			},
			handleItem(i) {
				this.active = i
				this.$emit('change', i)
				this.$emit('input', false)
			}
		}
	}
</script>
<style lang="less" scoped>
	.areaCode_com {
		::v-deep .van-popup {
			background-color: #1A1A1A;

			.van-action-sheet__header {
				text-align: left;
				color: #fff;
				padding-left: 16px;
			}

			.van-action-sheet__close {
				color: #fff;
			}
		}

		.content {
			color: #fff;
			padding: 0 15px;
			text-align: left;

			.list_box {
				overflow-y: auto;
				max-height: calc(80vh - 70px);

				.item_box {
					line-height: 50px;
					border-radius: 10px;
					text-align: left;
					padding: 0 15px;
					margin-bottom: 10px
				}

				.item_box:last-of-type {
					margin-bottom: 0;
				}

				.active {
					border: 1px solid #0DF5E3;
				}

				.no_data {
					text-align: center;
					padding: 20px;
				}
			}
		}
	}
</style>